<?php


namespace App\Http\Controllers\Api;


use App\Businesses\BusinessState;
use App\Constant\ApiV2Constant;
use App\Services\Business\Models\Category;
use App\Services\Business\Models\Group;
use App\Services\Business\Interfaces\GroupServiceInterface;
use App\Services\Business\Services\GroupService;
use App\Zesow\Cache\Impl\CompanyCategoryCache;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Services\UserService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class GroupController extends BaseController
{

    /**
     * @var UserService
     */

    protected $userService;
    /**
     * @var GroupService
     */

    protected $groupService;

    protected $businessState;

    public function __construct(
        GroupServiceInterface        $groupService,
        UserServiceInterface         $userService,
        BusinessState                $businessState
    )
    {


        $this->groupService = $groupService;
        $this->userService = $userService;
        $this->businessState = $businessState;


    }

    /**
     * @param Request $request
     * @return JsonResponse
     */

    public function index(Request $request)
    {


        $filter = $request->input();
        $filter['user_id'] = $this->check()?$this->id():0;
        $filter= arr1_clear($filter, ApiV2Constant::GROUP_FILTER_FIELD);
        $groups = $this->groupService->simplePage($filter);
        $categories = Category::query()->select(['id', 'name'])->orderBy('sort')->get();
        return $this->successData(compact('groups','categories'));
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function detail($id ,CompanyCategoryCache $CompanyCategoryCache)
    {

        $group = $this->groupService->get($id);
        $categories = $CompanyCategoryCache->tree($group['company_id']);
        $isBuy = $group['is_free'] ?? 0;
        $isFollowed = false;
        $isFavorited = false;
        if ($this->check()) {
            $userId= $this->id();
            $isBuy = $this->businessState->isBuyGroup($userId, $id);
            $isFollowed = $this->userService->isFollowed($userId, $group['user_id']);
            $isFavorited = $this->userService->isFavorited($userId, $id, Group::class);
        }
        $resources = ($this->groupService->hasResources($id)) ? $this->groupService->getResources($id) : [];
        return $this->successData(compact("group", 'resources', 'categories','isBuy','isFavorited','isFollowed'));
    }


    public function activities(Request $request, $id)
    {

        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $activities = $this->groupService->getActivities($id, $page, $size);
        return $this->data([
            'activities' => $activities,
        ]);
    }

    public function doubts(Request $request, $id)
    {
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $doubts = $this->groupService->getDoubts($id, $page, $size);
        return $this->data([
            'doubts' => $doubts,
        ]);
    }


}
